<template>
  <div class='list' ref='wrapper'>
    <div>
      <div class="area">
        <div class="title">当前城市</div>
        <div class="button-list">
          <div class='button-wrapper'>
            <div class="button">
              {{currentCity}}
            </div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title">热门城市</div>
        <div class="button-list">
          <div class='button-wrapper' v-for="(item, index) in hotCities" :key="index" @click="handleCityClick(item.name)">
            <div class="button">
              {{item.name}}
            </div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item, key) in cities" :key="key" :ref="key">
        <div class="title">{{key}}</div>
        <div class="item-list" v-for="(item, index) in item" :key="index">
          <div class='item' @click="handleCityClick(item.name)">
            {{item.name}}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from "better-scroll";
import { mapState } from "vuex";
import { mapMutations } from "vuex";
export default {
  props: {
    hotCities: Array,
    cities: Object,
    letter: String
  },
  methods: {
    handleCityClick(city, e) {
      // this.$store.commit("changeCity", city)
      this.changeCity(city)
      this.$router.push("/home")
    },
    ...mapMutations(['changeCity'])
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  mounted() {
    this.bscroll = new BScroll(this.$refs.wrapper, {
      mouseWheel: true,
      scrollbar: true,
    });
    this.bscroll.on("scroll", pos => { });
  },

  watch: {
    letter() {
      const element = this.$refs[this.letter][0];
      this.bscroll.scrollToElement(element);
    }
  },

};
</script>

<style lang="stylus" scoped>
.list {
  overflow: hidden;
  position: absolute;
  top: 1.68rem;
  left: 0px;
  right: 0px;
  bottom: 0px;

  .title {
    line-height: 0.44rem;
    background: #eee;
    padding-left: 0.2rem;
    margin-bottom: 1px;
    font-size: 0.26rem;
    color: #666;
  }

  .button-list {
    padding: 0.1rem 0.6rem 0.1rem 0.1rem;
    overflow: hidden;

    .button-wrapper {
      width: 33.33%;
      float: left;

      .button {
        margin: 0.1rem 0.1rem;
        padding: 0.1rem 0;
        text-align: center;
        border: 0.02rem solid #ccc;
        border-radius: 0.06rem;
      }
    }
  }

  .item-list {
    .item {
      line-height: 0.76rem;
      color: #666;
      padding-left: 0.2rem;
      border-bottom: 1px solid #ccc;
    }
  }
}
</style>
